//index.js
//获取应用实例
const app = getApp();
import store from "../../utils/factory";
import { getRecommendList, shopCarAdd } from "../../utils/config";
let that = null;

Page({
  data: {
    note: [],
    pageInfo: {
      page: 1,
      limit: 20,
      type: 2 //小类 1 大类
    }
  },
  onLoad: function() {
    that = this;
  },
  onShow() {
    this.getRecommendList();
  },
  returnIndex() {
    wx.switchTab({
      url: "../index/index"
    });
  },
  goDetail(e) {
    const id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: `../logs/logs?id=${id}`
    });
  },
  getRecommendList() {
    getRecommendList(this.data.pageInfo).then(res => {
      let req = res.data;
      that.setData({
        note: req
      });
    });
  },
  addShopCar: function(e) {
    console.log(e);
    if (!store.get("userInfo")) {
      wx.navigateTo({
        url: "../autoLogin/autoLogin" // 页面 B
      });
      return false;
    }
    let detailObj = e.currentTarget.dataset.obj;
    if (!detailObj) {
      wx.showToast({
        title: "该商品不存在",
        icon: "none"
      });
      return false;
    }
    if (detailObj.saleOut) {
      wx.showToast({
        title: "该商品已经售罄",
        icon: "none"
      });
      return false;
    }
    shopCarAdd({
      category: detailObj.id,
      quantity: 1
    }).then(res => {
      wx.showToast({
        title: "添加购物车成功",
        icon: "none",
        duration: 1000
      });
    });
  }
});
